package com.hw.core.system.controller;

import com.hw.core.system.entity.NoteBook;
import com.hw.core.system.entity.User;
import com.hw.core.system.service.NoteBookService;
import com.hw.core.system.service.UserService;
import com.hw.core.util.PasswordUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Date;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("register")
    public String register(@Valid User user, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            redirectAttributes.addFlashAttribute( "message", bindingResult.getFieldError().getField() + ":" + bindingResult.getFieldError().getDefaultMessage() );
            return "redirect:/register";
        }
        user.setCreateTime( new Date() );
        user.setPassword( PasswordUtil.getPasswordByUsernameAndPassword( user.getName(), user.getPassword() ) );
        user.setImg( "/img/tx/touxiang0.jpg" );
        userService.save( user );
        redirectAttributes.addFlashAttribute( "message", "注册成功，请登录！" );
        return "redirect:/login";

    }

    @PostMapping("resetPwd")
    @ResponseBody
    public String resetPwd(String oldPass, String newPass) {
        Subject currentUser = SecurityUtils.getSubject();
        User loginUser = (User) currentUser.getPrincipal();
        String name = loginUser.getName();
        if (loginUser.getPassword().equals(
                PasswordUtil.getPasswordByUsernameAndPassword( name, oldPass ) )) {
            userService.resetPwd( loginUser.getId(), PasswordUtil.getPasswordByUsernameAndPassword( name, newPass ) );
            currentUser.logout();
            return "密码成功修改为: " + newPass + "  请牢记！";
        } else {
            return "原密码错误";
        }
    }

    @PostMapping("resetNickName")
    @ResponseBody
    public String resetNickName(String nickname) {
        Subject currentUser = SecurityUtils.getSubject();
        User loginUser = (User) currentUser.getPrincipal();
        userService.resetNickName( loginUser.getId(), nickname );
        currentUser.logout();
        return "修改成功";

    }

    @PostMapping("resetImg")
    @ResponseBody
    public String resetImg(String img) {
        Subject currentUser = SecurityUtils.getSubject();
        User loginUser = (User) currentUser.getPrincipal();
        userService.resetImg( loginUser.getId(), img );
        currentUser.logout();
        return "修改成功";

    }
    @GetMapping("/")
    public String index(){
        return "user/index";

    }
    @GetMapping("/myself")
    public String myself(Model model){
        Subject currentUser = SecurityUtils.getSubject();
        User loginUser = (User) currentUser.getPrincipal();
        User u =userService.findUserById( loginUser.getId() );
        model.addAttribute( "user",u );
        return "user/myself";

    }



    @GetMapping("{name}")
    public String view(@PathVariable String name) {
        return "user/"+name;
    }

}
